package com.example.gymnasiumsystem.servlet;

import com.example.gymnasiumsystem.dao.GymInfoDAO;
import com.example.gymnasiumsystem.dao.UserInfoDAO;
import com.example.gymnasiumsystem.enity.GymInfo;
import com.example.gymnasiumsystem.enity.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/updateGym")
public class UpdateGymServlet extends HttpServlet {
    private UserInfoDAO userInfoDAO=new UserInfoDAO();
    private GymInfoDAO gymInfoDAO=new GymInfoDAO();
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");  // 设置请求字符编码为UTF-8
        resp.setContentType("text/html;charset=UTF-8");  // 设置响应字符编码为UTF-
        HttpSession session=req.getSession();
        UserInfo userInfo=(UserInfo) session.getAttribute("user");
        if(userInfo.getIsAdmin()==0){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("抱歉你没有这个权限");
            return;
        }
        String gymName=req.getParameter("gymName");
        String gymLocation=req.getParameter("gymLocation");
        String gymDescription=req.getParameter("gymDescription");
        GymInfo gymInfo=new GymInfo();
        gymInfo.setGymLocation(gymLocation);
        gymInfo.setGymDescription(gymDescription);
        gymInfo.setGymName(gymName);
        GymInfo gymInfo1=gymInfoDAO.getGymByName(gymName);
        gymInfo.setGymId(gymInfo1.getGymId());
        if(gymInfo1==null){
            gymInfoDAO.addGym(gymInfo);
            resp.sendRedirect("admin/gymList.jsp");
            return;
        }else{
            gymInfoDAO.updateGym(gymInfo);
            resp.sendRedirect("admin/gymList.jsp");
            return;
        }

    }
}
